package com.zcoj.controller.Admin.training;

import com.zcoj.core.model.ApiResponse;
import com.zcoj.core.model.PageWrap;
import com.zcoj.pojo.Dto.Training;
import com.zcoj.pojo.Dto.TrainingCategory;
import com.zcoj.pojo.Dto.TrainingProblem;
import com.zcoj.service.ProblemService;
import com.zcoj.service.TrainingCategoryService;
import com.zcoj.service.TrainingProblemService;
import com.zcoj.service.TrainingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author chenbin
 * @version 1.0
 * @description: TODO
 * @date 2023/4/19 09:21
 */
@RequestMapping("/adminTraining")
@RestController
@PreAuthorize("hasAnyAuthority('super_admin','admin','training_admin')")
public class adminTrainingController {


    @Autowired
    private TrainingService trainingService;

    @Autowired
    private TrainingProblemService trainingProblemService;

    @Autowired
    private TrainingCategoryService trainingCategoryService;


    @Autowired
    private ProblemService problemService;



    @PostMapping("/createTraining")
    public ApiResponse createTraining(@RequestBody Training training){
        Integer integer = trainingService.create(training);
        if(integer!=0){
            return ApiResponse.success("添加成功",integer);
        }else {
            return ApiResponse.failed("创建失败！！！");
        }
    }

    @GetMapping("/appendTrainingProblemData/{tid}")
    public ApiResponse appendContestProblemData(@PathVariable("tid") Integer tid,@RequestParam("pids") List<String> pids){
        Integer sum=0;
        for (String pid : pids) {
            TrainingProblem trainingProblem = new TrainingProblem();
            trainingProblem.setPid(Integer.parseInt(pid));
            trainingProblem.setTid(tid);
            trainingProblemService.create(trainingProblem);
            sum++;
        }
        return ApiResponse.success(sum+"条题目数据添加完成",null);
    }


    @GetMapping("/editTrainingProblemData/{tid}")
    public ApiResponse editContestProblemData(@PathVariable("tid") Integer tid,@RequestParam("pids") List<String> pids){
        Integer sum=0;
        TrainingProblem tp = new TrainingProblem();
        tp.setTid(tid);
        List<TrainingProblem> trainingProblemServiceList = trainingProblemService.findList(tp);
        for (TrainingProblem trainingProblem : trainingProblemServiceList) {
            trainingProblemService.deleteById(trainingProblem.getId());
        }
        for (String pid : pids) {
            TrainingProblem trainingProblem = new TrainingProblem();
            trainingProblem.setPid(Integer.parseInt(pid));
            trainingProblem.setTid(tid);
            trainingProblemService.create(trainingProblem);
            sum++;
        }
        return ApiResponse.success(sum+"条题目数据更新完成",null);
    }

    @PostMapping("/trainingList")
    public ApiResponse trainingList(@RequestBody PageWrap<Training> pageWrap){
        return ApiResponse.success(trainingService.findPage(pageWrap));
    }


    @GetMapping("/delTraining/{id}")
    public ApiResponse delTraining(@PathVariable Integer id){
        Integer integer = trainingService.deleteById(id);
        if(integer!=0){
            return ApiResponse.success("删除成功",null);
        }else {
            return ApiResponse.failed("删除失败！！！");
        }
    }


    @PostMapping("/updateTraining")
    public ApiResponse updateTraining(@RequestBody Training training){
        Integer updateById = trainingService.updateById(training);
        if(updateById!=0){
            return ApiResponse.success("更新成功",null);
        }else {
            return ApiResponse.failed("更新失败！！！");
        }
    }

    @GetMapping("/getTrainingById/{id}")
    public ApiResponse getTrainingById(@PathVariable Integer id){
        return ApiResponse.success( trainingService.findById(id));
    }

    @GetMapping("/selectedTrainingProblemList/{tid}")
    public ApiResponse selectedTrainingProblemList(@PathVariable Integer tid){
        TrainingProblem trainingProblem = new TrainingProblem();
        trainingProblem.setTid(tid);
        List<TrainingProblem> list = trainingProblemService.findList(trainingProblem);
        List<Integer> problemIdList=new ArrayList<>();
        for (TrainingProblem trainingProblem1 : list) {
            problemIdList.add(trainingProblem1.getPid());
        }
        return ApiResponse.success("",problemIdList);
    }


    @GetMapping("/getTrainingCategoryTable")
    public ApiResponse getTrainingCategoryTable(){
        return ApiResponse.success(trainingCategoryService.findList(new TrainingCategory()));
    }


    @GetMapping("/deleteTrainingCategoryById/{id}")
    public ApiResponse deleteTrainingCategoryById(@PathVariable Integer id){
        Integer integer = trainingCategoryService.deleteById(id);
        if(integer!=0){
            return ApiResponse.success("删除成功");
        }else {
            return ApiResponse.failed("删除失败");
        }

    }


    @PostMapping("/createTrainingCategory")
    public ApiResponse createTrainingCategory(@RequestBody TrainingCategory trainingCategory){
        if(Objects.nonNull(trainingCategory.getName())){
            trainingCategoryService.create(trainingCategory);
            if(Objects.nonNull(trainingCategory.getId())){
                return ApiResponse.success("创建成功");
            }else {
                return ApiResponse.failed("创建失败");
            }
        }else {
            return ApiResponse.failed("信息错误");
        }
    }


    @GetMapping("/findTrainingCategoryById/{id}")
    public ApiResponse findTrainingCategoryById(@PathVariable Integer id){
        TrainingCategory byId = trainingCategoryService.findById(id);
        if(Objects.nonNull(byId)){
            return ApiResponse.success(byId);
        }else {
            return ApiResponse.failed("无此DiscussionCategoryId");
        }

    }

    @PostMapping("/updateTrainingCategory")
    public ApiResponse updateTrainingCategory(@RequestBody TrainingCategory trainingCategory){
        Integer integer = trainingCategoryService.updateById(trainingCategory);
        if(integer!=0){
            return ApiResponse.success("更新成功");
        }else {
            return ApiResponse.failed("更新失败");
        }

    }
}
